<?php
/**
 * Класс для обработки заявки на участие в тендере
 *
 * @author kingop
 */
class FormProcessor_SendRequest extends FormProcessor {
    
    protected $db = null;
    public $request = null;
    
    public function __construct($db) {
        parent::__construct();
        
        $this->db = $db;
        $this->request = new DatabaseObject_Requests($this->db);
    }
    
    public function process(Zend_Controller_Request_Abstract $request) {
        // сохранить все это - ппц трешак, но мы это сделаем :)
        
        $this->request->idUser = $request->getPost('idUser');
        $this->request->idTender = $request->getPost('idTender');
        
        $this->firm_name = trim($request->getPost('firm_name'));
        if (strlen($this->firm_name) == 0)
            $this->addError('firm_name', 'Поле должно быть заполнено');
        else $this->request->firm_name = $this->firm_name;
        
        $this->firm_info = trim($request->getPost('firm_info'));
        if (strlen($this->firm_info) == 0)
            $this->addError('firm_info', 'Поле должно быть заполнено');
        else $this->request->firm_info = $this->firm_info;
        
        $this->firm_zavod = trim($request->getPost('firm_zavod'));
        if (strlen($this->firm_zavod) == 0)
            $this->request->firm_zavod = '';
        else $this->request->firm_zavod = $this->firm_zavod;
        
        $this->firm_manager_fio = trim($request->getPost('firm_manager_fio'));
        if (strlen($this->firm_manager_fio) == 0)
            $this->addError('firm_manager_fio', 'Поле должно быть заполнено');
        else $this->request->firm_manager_fio = $this->firm_manager_fio;
        
        $this->firm_face_info = trim($request->getPost('firm_face_info'));
        if (strlen($this->firm_face_info) == 0)
            $this->addError('firm_face_info', 'Поле должно быть заполнено');
        else $this->request->firm_face_info = $this->firm_face_info;
        
        $this->firm_ext_info = trim($request->getPost('firm_ext_info'));
        if (strlen($this->firm_ext_info) == 0)
            $this->addError('firm_ext_info', 'Поле должно быть заполнено');
        else $this->request->firm_ext_info = $this->firm_ext_info;
        
        if($request->getPost('idUR')) {
            
            $sourceRequest = new DatabaseObject_Requests($this->db);
            $sourceRequest->load($request->getPost('idUR'));
            
            // далее идет тупое копирование одной заявки в другую
            
            $this->request->gosreg_copy = $sourceRequest->gosreg_copy;
            $this->request->egrpou = $sourceRequest->egrpou;
            $this->request->nds = $sourceRequest->nds;
            $this->request->agree_face = $sourceRequest->agree_face;
            $this->request->ustav_copy = $sourceRequest->ustav_copy;
            $this->request->allow_docs = $sourceRequest->allow_docs;
            $this->request->pass_page = $sourceRequest->pass_page;
            $this->request->doc_director = $sourceRequest->doc_director;
            $this->request->bank_info = $sourceRequest->bank_info;
            $this->request->gni = $sourceRequest->gni;
            $this->request->suggest = $sourceRequest->suggest;
            $this->request->people = $sourceRequest->people;
            $this->request->is_soft = $sourceRequest->is_soft;
            $this->request->expr = $sourceRequest->expr;
            $this->request->recomend = $sourceRequest->recomend;
            $this->request->main_distr = $sourceRequest->main_distr;
            $this->request->tech = $sourceRequest->tech;
            
            $this->request->dtime = date("Y-m-d H:i:s");
            $this->request->isApproved = 0;
            $this->request->isDeleted = 0;
            $this->request->deleteReason = '';
                        
            if (!$this->_validateOnly && !$this->hasError()) {
                $this->request->save();
                
                // данные успешно слямзены, осталось скопировать файлы
            
                $sourceFiles = DatabaseObject_RequestFiles::GetRequestsFiles($this->db, $sourceRequest->getId());
                foreach($sourceFiles as $sF) {
                    $newFile = new DatabaseObject_RequestFiles($this->db);
                    $newFile->idRequest = $this->request->getId();
                    $newFile->filename = $sF->filename;
                    $newFile->name = $sF->name;
                    
                    $newFile->save();
                }
            }   
        }
        else {
            if($request->getPost('gosreg_copy')) {
                $this->request->gosreg_copy = 1;
                $this->gosreg_copy = 1;
            }
            else $this->request->gosreg_copy = 0;

            if($request->getPost('egrpou')) {
                $this->request->egrpou = 1;
                $this->egrpou = 1;
            }
            else $this->request->egrpou = 0;

            if($request->getPost('nds')) {
                $this->request->nds = 1;
                $this->nds = 1;
            }
            else $this->request->nds = 0;

            if($request->getPost('agree_face')) {
                $this->request->agree_face = 1;
                $this->agree_face = 1;
            }
            else $this->request->agree_face = 0;

            if($request->getPost('ustav_copy')) {
                $this->request->ustav_copy = 1;
                $this->ustav_copy = 1;
            }
            else $this->request->ustav_copy = 0;

            if($request->getPost('allow_docs')) {
                $this->request->allow_docs = 1;
                $this->allow_docs = 1;
            }
            else $this->request->allow_docs = 0;

            if($request->getPost('pass_page')) {
                $this->request->pass_page = 1;
                $this->pass_page = 1;
            }
            else $this->request->pass_page = 0;

            if($request->getPost('doc_director')) {
                $this->request->doc_director = 1;
                $this->doc_director = 1;
            }
            else $this->request->doc_director = 0;

            if($request->getPost('bank_info')) {
                $this->request->bank_info = 1;
                $this->bank_info = 1;
            }
            else $this->request->bank_info = 0;

            if($request->getPost('gni')) {
                $this->request->gni = 1;
                $this->gni = 1;
            }
            else $this->request->gni = 0;

            if($request->getPost('gni')) {
                $this->request->gni = 1;
                $this->gni = 1;
            }
            else $this->request->gni = 0;

            if($request->getPost('suggest')) {
                $this->request->suggest = 1;
                $this->suggest = 1;
            }
            else $this->request->suggest = 0;

            if($request->getPost('people')) {
                $this->request->people = 1;
                $this->people = 1;
            }
            else $this->request->people = 0;

            if($request->getPost('is_soft')) {
                $this->request->is_soft = 1;
                $this->is_soft = 1;
            }
            else $this->request->is_soft = 0;

            if($request->getPost('expr')) {
                $this->request->expr = 1;
                $this->expr = 1;
            }
            else $this->request->expr = 0;

            if($request->getPost('recomend')) {
                $this->request->recomend = 1;
                $this->recomend = 1;
            }
            else $this->request->recomend = 0;

            if($request->getPost('main_distr')) {
                $this->request->main_distr = 1;
                $this->main_distr = 1;
            }
            else $this->request->main_distr = 0;

            if($request->getPost('tech')) {
                $this->request->tech = 1;
                $this->tech = 1;
            }
            else $this->request->tech = 0;

            $this->request->dtime = date("Y-m-d H:i:s");
            $this->request->isApproved = 0;
            $this->request->isDeleted = 0;
            $this->request->deleteReason = '';

           $this->file = $request->getPost('filesrc');
            if (count($this->file) == 0)
                $this->addError('file', 'Загрузите файл');
            else $this->request->file = $this->file;

            $this->file = $request->getPost('filesrc');

            $this->fileupload = $this->file;

            if (!$this->_validateOnly && !$this->hasError()) {
                $this->request->save();
                $id = $this->request->getId();
                  foreach ($this->fileupload as $val) {
                    if (DatabaseObject_RequestFiles::UploadFile($this->db, $id, $val))

                       echo 'Yes'; 

                    echo 'No';
                    }
            }
        }
        return !$this->hasError();
    }
}

?>